package com.ylz.sjzx.sio.common.core.core.container;

import com.ylz.sjzx.sio.common.core.core.Interceptor.InterceptorChain;

/**
 * 单例管理类
 */
public class ContainerManager {

    /**
     * 适配器容器
     */
    private static volatile AdapterContainer adapterContainer;

    /**
     * 拦截器容器
     */
    private static volatile InterceptorChain interceptorChain;

    /**
     * 初始化处理器链路，为了保证能在项目中兼容Spring容器管理初始化实例变量
     * 这里统一将API核心包中所有单例的初始化放置在Spring容器初始化完成之后。
     * 此方法。
     */
    public static void init() {
        initAdapterContainer();
        initInterceptorChain();
    }

    /**
     * @return AdapterContainer
     */
    public static AdapterContainer getAdapterContainer() {
        if (adapterContainer == null) {
            initAdapterContainer();
        }
        return adapterContainer;
    }

    /**
     * 初始化AdapterContainer
     */
    private static void initAdapterContainer() {
        if (adapterContainer == null) {
            synchronized (AdapterContainer.class) {
                if (adapterContainer == null) {
                    adapterContainer = new AdapterContainer();
                }
            }
        }
    }

    /**
     * @return InterceptorChain
     */
    public static InterceptorChain getInterceptorChain() {
        if (interceptorChain == null) {
            initInterceptorChain();
        }

        return interceptorChain;
    }

    /**
     * 初始化InterceptorChain
     */
    private static void initInterceptorChain() {

        if (interceptorChain == null) {
            synchronized (InterceptorChain.class) {
                if (interceptorChain == null) {
                    interceptorChain = new InterceptorChain();
                }
            }
        }
    }

}
